<template>
    <l-layout class="l-tab-page" :top="formHeight + 12">
        <template #top>
            <l-query :span="5" :labelWidth="150" :height.sync="formHeight" :items="awaitAssessFormColmuns"
                :formData="awaitAssessFormData" :loading="tableLoading" @search="handleSearch">
                <template #updateCycleCode>
                    <l-select :options="update_cycle_code_options" v-model="awaitAssessFormData['updateCycleCode']"
                        @change="handleSearch" />
                </template>
                <template #regionCode>
                    <l-select :options="region_code_options" v-model="awaitAssessFormData['regionCode']"
                        @change="handleSearch" />
                </template>
                <template #name>
                    <el-input v-model="awaitAssessFormData['name']" :placeholder="$t('请输入')" clearable
                        @change="handleSearch" />
                </template>
                <template #muTaxQRange>
                    <l-number-range v-model="awaitAssessFormData['muTaxQRange']" @change="handleSearch"></l-number-range>
                </template>
                <!-- <template #comprehensiveEvaluationCode>
                    <l-select :options="comprehensive_evaluation_options" v-model="awaitCashFormData['comprehensiveEvaluationCode']"
                        @change="handleSearch" />
                </template> -->
            </l-query>
        </template>
        <l-panel style="padding-top:0;">
            <template #toolLeft>
                <div class="l-panel--item">
                    <l-excel-btns></l-excel-btns>
                </div>
            </template>
            <template #toolRight>
                <l-tool-btns :hasAdd="false" :hasTableSetting="true" @setting="lr_handleSettingTable">
                </l-tool-btns>
            </template>
            <l-table ref="mainTable" :loading="tableLoading" :columns="awaitAssessTableColmuns" :dataSource="tableData"
                :isPage="true" :pageTotal="tableTotal" :tablePage.sync="tableCurrentPage" @loadPageData="turnTablePage">
                <template #name="scope">
                    <a @click="handleDetails(0, scope.row)">{{ scope.row.name || '---' }}</a>
                </template>
                <template v-for="(col, index) in lr_tableColumnsByToLabel(awaitAssessTableColmuns)"
                    v-slot:[col.prop]="scope">
                    <l-value-to-label :value="scope.row[col.prop]" :type="col.dataType" :code="col.dataCode"
                        :valueKey="col.valueKey" :labelKey="col.labelKey" :options="col.options" :format="col.format"
                        :setting="col.setting" :key="index"></l-value-to-label>
                </template>
                <l-table-btns :btns="tableBtns" :filterBtns="filterBtns" @click="lr_handleTableBtnClick"
                    :btnNum="6"></l-table-btns>
            </l-table>
        </l-panel>
        <!-- <AssessDialog ref="assessDialog" @refresh="loadTableData" /> -->
        <AssessDialog ref="assessDialog" @refresh="handleRefresh" />
        <DetailDialogForm ref="DetailDialogForm" />
    </l-layout>
</template>

<!-- 待评定 -->
<script>
import AssessDialog from "./AssessDialog"
import DetailDialogForm from "../../../enterpriseManagement/views/enterpriseList/DetailDialogForm/index.vue"
import {
    awaitAssessFormColmuns,
    awaitAssessTableColmuns,
    awaitAssessFormData,
    awaitAssessapi,
} from "./columns";
export default {
    data() {
        return {
            formHeight: 44,
            awaitAssessFormData,
            awaitAssessFormColmuns,
            tableLoading: true,
            tableData: [],
            tableTotal: 0,
            tableCurrentPage: 1,
            tablePageSize: 50,
            awaitAssessTableColmuns,
            tableBtns: [{ prop: "Assess", label: "评定" }],
        };
    },
    components: {
        AssessDialog,
        DetailDialogForm
    },
    computed: {
        selectId() {
            return this.selectFile.f_Id || "";
        },
        update_cycle_code_options() {
            return this.lr_dataItemOptions(this.lr_dataItem["update_cycle_code"]);
        },
        region_code_options() {
            return this.lr_dataItemOptions(this.lr_dataItem["region_code"]);
        },
    },
    mounted() {
        this.$watch(
            () => this.awaitAssessFormData.muTaxQRange,
            (newVal) => {
                if (newVal === "") {
                    this.awaitAssessFormData.muTaxQRange = null;
                }
            }
        )
        this.init();
    },
    methods: {
        async handleRefresh() {
            await this.loadTableData()
            this.$emit("refresh")
        },
        handleDetails(index, row) {
            this.$refs.DetailDialogForm.open(row);
        },
        // 评定
        handleAssess(index, row) {
            const currentDate = new Date();
            const year = currentDate.getFullYear();
            const month = String(currentDate.getMonth() + 1).padStart(2, '0');
            const day = String(currentDate.getDate()).padStart(2, '0');
            const formattedDate = `${year}-${month}-${day}`;
            this.$set(this.$refs.assessDialog.awaitAssessDialogFormData, 'evaluationDate', formattedDate);
            this.$refs.assessDialog.open(row);
        },
        init() {
            this.loadTableData();
        },
        filterBtns(row, btns) {
            return btns;
        },
        handleSearch() {
            this.loadTableData();
        },
        turnTablePage({ rows }) {
            this.tablePageSize = rows;
            this.loadTableData(true);
        },
        async loadTableData(isNotFirst) {
            if (!isNotFirst) {
                this.tableCurrentPage = 1;
            }
            this.tableLoading = true;
            let awaitAssessFormData = this.$deepClone(this.awaitAssessFormData);
            awaitAssessFormData.rows = this.tablePageSize;
            awaitAssessFormData.page = this.tableCurrentPage;
            awaitAssessFormData.sidx = "modifyTime";
            awaitAssessFormData.sord = "DESC";
            let data = await this.$awaitWraper(
                awaitAssessapi.getPage(awaitAssessFormData)
            );
            if (data != null) {
                this.tableData = data.rows;
                this.tableTotal = data.records;
            } else {
                this.tableTotal = 0;
                this.tableData = [];
            }
            this.tableLoading = false;
        },
    }
}
</script>


<style scoped lang='scss'></style>